package com.china08.yxyapi.api.manage.sch.platform.xiaoben.auth;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.service.db.UserAuthService;
import com.china08.yxyapi.util.DateToolUtils;

@CrossOrigin
@RestController
public class UserAuthUserKeChengList extends YxyApiBaseController {

    @Autowired
    UserAuthService userAuthServiceImpl;

    @Autowired
    YxyLessonsSchoolCatalogRepository catalogRepository;

    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/auth/user/kecheng/user/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String userId = reqModel.userId;
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;
        String keyword = StringUtils.isNotBlank(reqModel.keyword) ? "%" + reqModel.keyword + "%" : "";
        Integer pageNum = reqModel.pageNum;
        Integer status = reqModel.status;
        List<Pojo> returnList = new ArrayList<>();

        Integer allPage = 0;
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = userAuthServiceImpl.userAuthKeChengList(userId, catalogId1, catalogId2, keyword, pageNum, status);
        if (null != map) {
            allPage = Integer.parseInt(map.get("allPage") + "");
            list = (List<Map<String, Object>>) map.get("list");
        }

        for (Map<String, Object> m : list) {
            Pojo pojo = new Pojo();
            pojo.userId = m.get("user_id") + "";
            pojo.lessonId = m.get("lesson_id") + "";
            pojo.name = m.get("name") + "";
            String cId1 = m.get("catalog_id1") + "";
            String cId2 = m.get("catalog_id2") + "";
            YxyLessonsSchoolCatalog catalog1 = catalogRepository.findById(cId1);
            YxyLessonsSchoolCatalog catalog2 = catalogRepository.findById(cId2);
            if (catalog1 != null) {
                pojo.catalogName1 = catalog1.getName();
            }
            if (catalog2 != null) {
                pojo.catalogName2 = catalog2.getName();
            }
            if (m.get("count") != null && StringUtils.isNotBlank(m.get("count") + "")) {
                pojo.count = m.get("count") + "";
            }
            if (m.get("author") != null && StringUtils.isNotBlank(m.get("author") + "")) {
                pojo.author = m.get("author") + "";
            }
            Object obj1 = m.get("created_date");
            if (obj1 != null) {
                pojo.createdDate = DateToolUtils.format((Date) obj1, "yyyy-MM-dd HH:mm:ss");
            }
            returnList.add(pojo);
        }
        RespModel respModel = new RespModel();
        respModel.allPage = allPage;
        respModel.list = returnList;
        return respModel;
    }

    public static class Pojo {
        public String userId;
        public String lessonId;
        public String name;
        public String catalogName1;
        public String catalogName2;
        public String count = "0";
        public String author = "";
        public String createdDate;
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String userId;
        public String catalogId1;
        public String catalogId2;
        public String keyword;
        public Integer status;
        public Integer pageNum;
    }

    public static class RespModel extends YxyApiRespModel {
        public Integer allPage;
        public List<Pojo> list;
    }
}
